/*
 * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
 *
 * Licensed under the Aduna BSD-style license.
 */

package edu.tju.commons.collections.iterators;

import java.util.Iterator;

/**
 * Abstract superclass for iterators that wrap another iterator. The abstract
 * class <tt>IteratorWrapper</tt> itself provides default methods that forward
 * method calls to the wrapped iterator, closing itself when exhausted.
 * Subclasses of <tt>IteratorWrapper</tt> should override some of these methods
 * and may also provide additional methods and fields.
 */
public class IteratorWrapper<E> extends CloseableIteratorBase<E> {

	/**
	 * The wrapped iterator.
	 */
	protected final Iterator<? extends E> wrappedIter;

	/**
	 * Creates a new IteratorWrapper that operates on the supplied iterator.
	 * 
	 * @param iter
	 *        The wrapped iterator for this <tt>IteratorWrapper</tt>, must not be
	 *        <tt>null</tt>.
	 */
	public IteratorWrapper(Iterator<? extends E> iter) {
		assert iter != null;
		wrappedIter = iter;
	}

	public boolean hasNext() {
		return wrappedIter.hasNext();
	}

	public E next() {
		return wrappedIter.next();
	}

	public void remove() {
		wrappedIter.remove();
	}

	@Override
	protected void handleClose() {
		super.handleClose();
		Iterators.closeCloseable(wrappedIter);
	}
}
